package com.bsit.obu.utils;

import android.text.TextUtils;
import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dreamer on 2016/11/28.
 */

public class XMLUtils {

    /**
     * 解析XML转换成对象
     *
     * @param str
     *            字符串
     * @return
     */
    public static String parse(String str) {
        InputStream is = getStringStream(str);
        String result = "";
        Log.v("rss", "开始解析XML.");
        List<Object> list = new ArrayList<>();
        try {
            XmlPullParser xmlPullParser = Xml.newPullParser();
            xmlPullParser.setInput(is, "UTF-8");
            int event = xmlPullParser.getEventType();

            Object obj = null;

            while (event != XmlPullParser.END_DOCUMENT) {
                switch (event) {
                    case XmlPullParser.START_TAG:
                        if ("return".equals(xmlPullParser.getName())) {
                            result = xmlPullParser.nextText();
                        }

                        break;
                    case XmlPullParser.END_TAG:
                        break;
                }
                event = xmlPullParser.next();
            }
        } catch (Exception e) {
            Log.e("rss", "解析XML异常：" + e.getMessage());
            throw new RuntimeException("解析XML异常：" + e.getMessage());
        }
        return result;
    }

    /**
     * 设置字段值
     *
     * @param propertyName
     *            字段名
     * @param obj
     *            实例对象
     * @param value
     *            新的字段值
     * @return
     */
    public static void setFieldValue(Object obj, String propertyName,
                                     Object value) {
        try {
            Field field = obj.getClass().getDeclaredField(propertyName);
            field.setAccessible(true);
            field.set(obj, value);
        } catch (Exception ex) {
            throw new RuntimeException();
        }
    }

    /**
     　　* 将一个字符串转化为输入流
     　　*/
    public static InputStream getStringStream(String sInputString){
         if (sInputString != null && !sInputString.trim().equals("")){
             try{
                 ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(sInputString.getBytes());
                 return tInputStringStream;
                 }catch (Exception ex){
                 ex.printStackTrace();
                 }
             }
         return null;
    }

    /**
     * 将接收到的字符串转化为map对象，可根据属性名取到相应的值
     * @param str
     * @return
     */
    public static Map<String,String> parseString2Map(String str){
        Map<String, String> map = new HashMap<>();
        str = str.substring(str.indexOf("{"), str.indexOf("}"));
        String[] result = str.split(";");
        for (int i = 0; i < result.length; i++) {
            if (!result[i].contains("=")) {
                continue;
            }
            String data[] = result[i].split("=");
            if (!TextUtils.isEmpty(data[0]) && !TextUtils.isEmpty(data[1])) {
                System.out.println(data[0] + "     " + data[1]);
                map.put(data[0].trim(), data[1].trim());
            }

        }
        return map;
    }
}
